package com.tianya.common.report;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.tianya.common.model.SysInfo;
import org.apache.struts2.ServletActionContext;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;  
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign; 
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import java.net.URL;  
public class JRPrinterApplet {
		/** Creates new form AppletViewer */     
	  private URL url = null;      
	  private JasperPrint jasperPrint = null;  
		public URL getUrl() {
		return url;
	}
	public void setUrl(URL url) {
		this.url = url;
	}
	public JasperPrint getJasperPrint() {
		return jasperPrint;
	}
	public void setJasperPrint(JasperPrint jasperPrint) {
		this.jasperPrint = jasperPrint;
	}     
	     /**    
	     *    
	     */ 
	/**
	 * 生成print文件
	 */
	     @SuppressWarnings("unchecked")
		public JasperPrint getjrt(HashMap hm,List<Map> li,Integer flag) throws JRException{
	    		String sample=null;
	    	 	if(flag==1){
	    	 		sample	=ServletActionContext.getServletContext().getRealPath("WEB-INF/reportTemplate/teamindex.jrxml");
	    	 	}
	    		if(flag==3){
	    			sample	=ServletActionContext.getServletContext().getRealPath("WEB-INF/reportTemplate/ticket.jrxml");
	    	 	}
	    		if(flag==4){
	    			sample	=ServletActionContext.getServletContext().getRealPath("WEB-INF/reportTemplate/teamticket.jrxml");
	    	 	}
	    		if(flag==5){
	    	 		sample	=ServletActionContext.getServletContext().getRealPath("WEB-INF/reportTemplate/total.jrxml");
	    	 	}
			    JasperDesign design=JRXmlLoader.load(sample);
			    new JasperCompileManager();
				JasperReport report=JasperCompileManager.compileReport(design);
			    JasperPrint theprint=JasperFillManager.fillReport(report,hm,new JRBeanCollectionDataSource(li));
			    return theprint;
	     }
	     
	     /**
	      * 生成PDF
	      * @param hm
	      * @param li
	      * @param flag
	      * @param name
	      * @throws JRException
	      */
	     @SuppressWarnings("unchecked")
		public void createpdf(HashMap hm,List<Map> li,Integer flag,String name) throws JRException {
	    	 	JasperPrint theprint=getjrt(hm,li,flag);
	    	 	JasperExportManager.exportReportToPdfFile(theprint, SysInfo.getTemporary()+name+".pdf");	
	     	}   
		}